package com.yanqu.road.server.manager.activity.horserun;

import com.yanqu.road.logic.helper.UnionActivityGroupHelper;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossHorseRunGroup {
    private int activityId;
    private Map<Integer,CrossHorseRunActivity> activityMap = new ConcurrentHashMap<>();

    public CrossHorseRunGroup(int activityId) {
        this.activityId = activityId;
    }

    public Map<Integer, CrossHorseRunActivity> getActivityMap() {
        return activityMap;
    }

    public void setActivityMap(Map<Integer, CrossHorseRunActivity> activityMap) {
        this.activityMap = activityMap;
    }

    public int getActivityId() {
        return activityId;
    }

    public void setActivityId(int activityId) {
        this.activityId = activityId;
    }

    public CrossHorseRunActivity getActivity(int groupId) {
        if (groupId == UnionActivityGroupHelper.NO_GROUP_ID) {
            return null;
        }
        CrossHorseRunActivity activity = activityMap.get(groupId);
        if (activity == null) {
            synchronized (activityMap) {
                activity = activityMap.get(groupId);
                if (activity == null) {
                    activity = new CrossHorseRunActivity(activityId, groupId);
                    activityMap.put(groupId, activity);
                }
            }
        }
        return activity;
    }

    public void save(){
        for (CrossHorseRunActivity activity : activityMap.values()) {
            activity.save();
        }
    }


    public void timerTask(){
        if (activityMap==null|| activityMap.isEmpty()){
            return;
        }
        for (CrossHorseRunActivity activity:activityMap.values()){
            try {
                activity.roundTask();
            }catch (Exception e){
                CrossHorseRunMgr.getLogger().error("timer error at activityId{} ,exception{}",activityId,e.getMessage());
            }
        }
    }
}
